package edu.hcmus.sow.service;

import edu.hcmus.sow.dao.FileDAO;

import edu.hcmus.sow.domain.File;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for File entities
 * 
 */

@Service("FileService")
@Transactional
public class FileServiceImpl implements FileService {

   /**
    * DAO injected by Spring that manages File entities
    * 
    */
   @Autowired
   private FileDAO fileDAO;

   /**
    * Instantiates a new FileServiceImpl.
    * 
    */
   public FileServiceImpl() {
   }

   /**
    * Load an existing File entity
    * 
    */
   @Transactional
   public List<File> findAll() {
      return fileDAO.findAll();
   }

   /**
	 */
   @Transactional
   public File findFileById(Integer id) {
      return fileDAO.findById(id);
   }

   /**
    */
   @Transactional
   public File findFileByName(String fileName) {
      return fileDAO.findByName(fileName);
   }

   /**
    * Save an existing File entity
    * 
    */
   @Transactional
   public void saveFile(File file) {
      File existingFile = fileDAO.findById(file.getId());

      if (existingFile != null) {
         if (existingFile != file) {
            existingFile.setId(file.getId());
            existingFile.setName(file.getName());
         }
         file = fileDAO.merge(existingFile);
      } else {
         file = fileDAO.persist(file);
      }
   }

   /**
    * Delete an existing File entity
    * 
    */
   @Transactional
   public void deleteFile(File file) {
      fileDAO.delete(file);
   }
}
